package com.sc.nft.service.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.dao.UserInfoDao;
import com.sc.nft.dao.WorkServiceWhiteDao;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.WorkServiceWhite;
import com.sc.nft.entity.dto.AddWhiteDTO;
import com.sc.nft.entity.dto.WhiteListDTO;
import com.sc.nft.entity.vo.WhiteListVO;
import com.sc.nft.enums.WhiteListEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.service.WorkServiceWhiteService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 城市服务中心和社区白名单考核(WorkServiceWhite)表服务实现类
 *
 * @author 王伟成
 * @since 2024-04-08 16:59:46
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WorkServiceWhiteServiceImpl implements WorkServiceWhiteService {

    private final WorkServiceWhiteDao workServiceWhiteDao;
    private final UserInfoDao userInfoDao;
    /**
     * 添加用户到白名单
     * @param addWhiteDTO
     */
    @Override
    public void addWhite(AddWhiteDTO addWhiteDTO) {
        UserInfo userInfo = userInfoDao.getById(addWhiteDTO.getUserId());
        if (BeanUtil.isEmpty(userInfo)) {
            throw new GlobalRunTimeException("用户不存在,无法开通白名单");
        }
        WorkServiceWhite workServiceWhite = workServiceWhiteDao.getByUserId(addWhiteDTO.getUserId());
        if (BeanUtil.isNotEmpty(workServiceWhite)) {
            throw new GlobalRunTimeException("用户已在白名单中,无需重复添加");
        }
        WorkServiceWhite save=new WorkServiceWhite();
        save.setUserId(addWhiteDTO.getUserId());
        workServiceWhiteDao.save(save);
    }
    /**
     * 移除白名单
     * @param id
     */
    @Override
    public void removeWhite(Long id) {
        if (!workServiceWhiteDao.removeByUserId(id)) {
            throw new GlobalRunTimeException("白名单移除失败,该白名单已不存在");
        }
    }
    /**
     * 白名单列表
     * @param whiteListDTO
     * @return
     */
    @Override
    public IPage<WhiteListVO> whiteList(WhiteListDTO whiteListDTO) {
        return workServiceWhiteDao.whiteList(whiteListDTO);
    }
    /**
     * 筛选条件
     * @return
     */
    @Override
    public Map<String, String> screenCondition() {
        // 枚举类WhiteListEnum转换为Map的形式,Key为枚举的字段,Value为枚举的description
        return WhiteListEnum.getAll();
    }
}
